package com.sinosoft.emplmis.application.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.application.web.dto.DictCommonData;
import com.sinosoft.emplmis.application.web.dto.DictSingleData;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.dict.domain.DictCommonEntity;
import com.sinosoft.framework.dict.service.DictService;

/**
 * 字典
 * 
 * @author Sun
 * 
 */
@Controller
@RequestMapping("application/dict")
public class DictController {

	@Autowired
	private DictService dictService;

	/**
	 * 根据字典表名查询单字典明细
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping(value = "/getSingleDictDataByTableName", method = RequestMethod.POST)
	@ResponseBody
	public DictSingleData getSingleDictDataByTableName(String tableName) {
		DictSingleData result = new DictSingleData();
		if (StringUtil.isBlank(tableName)) {
			return null;
		}
		List<DictCommonData> dictCommonDataList = new ArrayList<DictCommonData>();
		List<DictCommonEntity> entityList = dictService
				.findSingleDictDetail(tableName);
		for (DictCommonEntity entity : entityList) {
			DictCommonData data = new DictCommonData();
			data.toDictCommonData(data, entity);
			dictCommonDataList.add(data);
		}
		result.setTableName(tableName);
		result.setDictCommonDataList(dictCommonDataList);
		return result;
	}
	
	/**
	 * 根据字典表名和code查询name
	 * 
	 * @param tableName,code
	 * @return
	 */
	@RequestMapping(value = "/getNameByDictCode", method = RequestMethod.POST)
	@ResponseBody
	public String getNameByDict(String tableName, String code) {
		
		if (StringUtil.isBlank(tableName) || StringUtil.isBlank(code)) {
			return null;
		}
		List<DictCommonEntity> entityList = dictService
				.findSingleDictDetail(tableName);
		for (DictCommonEntity entity : entityList) {
			if(code.equals(entity.getCode())){
				return entity.getName();
			}
		}
		
		return "";
	}
	
	/**
	 * 根据字典表名和name查询code
	 * 
	 * @param tableName,name
	 * @return
	 */
	@RequestMapping(value = "/getCodeByDictName", method = RequestMethod.POST)
	@ResponseBody
	public String getCodeByDictName(String tableName, String name) {
		
		if (StringUtil.isBlank(tableName) || StringUtil.isBlank(name)) {
			return null;
		}
		List<DictCommonEntity> entityList = dictService
				.findSingleDictDetail(tableName);
		for (DictCommonEntity entity : entityList) {
			if(name.equals(entity.getName())){
				return entity.getCode();
			}
		}
		
		return "";
	}
	
	/**
	 * 实现级联菜单：根据父菜单的code做子菜单的parent_code返回子菜单的集合
	 * 
	 * @param tableName,code
	 * @return
	 */
	@RequestMapping(value = "/getSubMenu", method = RequestMethod.POST)
	@ResponseBody
	public List<DictCommonData> getSubMenu(String tableName, String parent_code) {
		
		if (StringUtil.isBlank(tableName) || StringUtil.isBlank(parent_code)) {
			return null;
		}
		List<DictCommonData> dictCommonDataList = new ArrayList<DictCommonData>();
		
		List<DictCommonEntity> entityList = dictService
				.findSingleDictDetail(tableName);
		for (DictCommonEntity entity : entityList) {
			if(parent_code.equals(entity.getParentCode())){
				DictCommonData data = new DictCommonData();
				data.toDictCommonData(data, entity);
				dictCommonDataList.add(data);
			}
		}
		
		return dictCommonDataList;
	}

	/**
	 * 根据字典表名查询字典明细数据
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping(value = "/getDictCommonDataByTableName", method = RequestMethod.POST)
	@ResponseBody
	public List<DictCommonData> getDictCommonDataByTableName(String tableName) {
		List<DictCommonData> dictCommonDataList = new ArrayList<DictCommonData>();
		if (StringUtil.isBlank(tableName)) {
			return dictCommonDataList;
		}
		List<DictCommonEntity> entityList = dictService
				.findSingleDictDetail(tableName);
		for (DictCommonEntity entity : entityList) {
			DictCommonData data = new DictCommonData();
			data.toDictCommonData(data, entity);
			dictCommonDataList.add(data);
		}
		return dictCommonDataList;
	}

	/**
	 * 根据一组字典表名查询多个字典
	 * 
	 * @param tableNameSet
	 *            表名字符串，表名之间用","分隔
	 * @return
	 */
	@RequestMapping(value = "/getMultiDictDataByTableNames", method = RequestMethod.POST)
	@ResponseBody
	public List<DictSingleData> getMultiDictDataByTableNames(String tableNameSet) {
		List<DictSingleData> resultList = new ArrayList<DictSingleData>();
		if (StringUtil.isBlank(tableNameSet)) {
			return null;
		}
		String[] tableNames = tableNameSet.split(",");
		List<DictCommonEntity> entityList = dictService
				.findMultiDictDetail(tableNames);
		Map<String, List<DictCommonEntity>> map = new HashMap<String, List<DictCommonEntity>>();
		for (DictCommonEntity entity : entityList) {
			List<DictCommonEntity> tempList = map.get(entity.getTableName());
			if (tempList == null) {
				tempList = new ArrayList<DictCommonEntity>();
				map.put(entity.getTableName(), tempList);
			}
			tempList.add(entity);
		}
		for (String tableName : tableNames) {
			DictSingleData result = new DictSingleData();
			List<DictCommonData> dictCommonDataList = new ArrayList<DictCommonData>();
			List<DictCommonEntity> tempList = map.get(tableName);
			if (tempList != null) {
				for (DictCommonEntity entity : tempList) {
					DictCommonData data = new DictCommonData();
					data.toDictCommonData(data, entity);
					dictCommonDataList.add(data);
				}
			}
			result.setTableName(tableName);
			result.setDictCommonDataList(dictCommonDataList);
			resultList.add(result);
		}
		return resultList;
	}
}
